﻿Namespace B1Assistant
    Public Class Time
        ''' <summary>
        ''' 计算两个日期间的时间差
        ''' </summary>
        ''' <param name="fromDate">开始日期</param>
        ''' <param name="fromTime">开始时间</param>
        ''' <param name="toDate">结束日期</param>
        ''' <param name="toTime">结束时间</param>
        ''' <param name="DurationUnit">时间差单位（D-Day，H-Hour，M-Minute）</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function ComputeDateDuration(ByVal fromDate As DateTime, ByVal fromTime As Integer, ByVal toDate As DateTime, ByVal toTime As Integer, ByVal DurationUnit As String) As Double
            'Dim ts As TimeSpan
            Dim hour As Integer = CInt(fromTime) \ 100
            Dim minute As Integer = fromTime - hour * 100
            'ts = New TimeSpan(hour, minute, 0)
            'fromDate.Add(ts)
            fromDate = fromDate.AddHours(hour)
            fromDate = fromDate.AddMinutes(minute)
            hour = CInt(toTime) \ 100
            minute = toTime - hour * 100
            'ts = New TimeSpan(hour, minute, 0)
            'toDate.Add(ts)
            toDate = toDate.AddHours(hour)
            toDate = toDate.AddMinutes(minute)
            Return ComputeDateDuration(fromDate, toDate, DurationUnit)
        End Function

        ''' <summary>
        ''' 计算两个日期间的时间差
        ''' </summary>
        ''' <param name="fromDate"></param>
        ''' <param name="toDate"></param>
        ''' <param name="DurationUnit">时间差单位（D-Day，H-Hour，M-Minute）</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function ComputeDateDuration(ByVal fromDate As DateTime, ByVal toDate As DateTime, ByVal DurationUnit As String) As Double
            Dim Duration As Double = 0
            If fromDate = Nothing Then
                Return 0
            End If
            If fromDate = DateTime.MinValue Then
                Return 0
            End If
            If toDate = Nothing Then
                Return 0
            End If
            If toDate = DateTime.MinValue Then
                Return 0
            End If
            Dim DateDiff As System.TimeSpan = toDate.Subtract(fromDate)
            Select Case DurationUnit
                Case "H"
                    Duration = DateDiff.TotalHours
                    Return Duration
                Case "M"
                    Duration = DateDiff.TotalMinutes
                    Return Duration
                Case "D"
                    Duration = DateDiff.TotalDays + 1
                    Return Duration
                Case Else
                    Return 0
            End Select

        End Function

        Public Shared Function GetDateTime(DateString As String, TimeString As String) As DateTime
            Dim year As String
            Dim month As String
            Dim day As String
            Dim tmpDate As DateTime = DateTime.MinValue
            Dim hour As Integer
            Dim minute As Integer
            Dim tmpTime As Integer
            If DateString.Length = 8 Then
                year = DateString.Substring(0, 4)
                month = DateString.Substring(4, 2)
                day = DateString.Substring(6, 2)
                tmpDate = Convert.ToDateTime(String.Format("{0}-{1}-{2}", year, month, day))
            End If
            Try
                tmpTime = Convert.ToInt32(TimeString)
                hour = Math.Floor(tmpTime / 100)
                minute = tmpTime - hour * 100
            Catch ex As Exception
                hour = 0
                minute = 0
            End Try
            tmpDate = tmpDate.AddHours(hour)
            tmpDate = tmpDate.AddMinutes(minute)
            Return tmpDate
        End Function
    End Class
End Namespace

